package com.xyzh05.webapp.action;

import java.util.List;

import com.opensymphony.xwork2.Preparable;
import com.xyzh05.model.Material;
import com.xyzh05.model.MaterialInfo;
import com.xyzh05.model.VipService;
import com.xyzh05.service.MajorManager;
import com.xyzh05.service.MaterialInfoManager;
import com.xyzh05.service.MaterialManager;
import com.xyzh05.service.SchoolManager;
import com.xyzh05.service.VipServiceManager;
import com.xyzh05.util.PageBean;

public class MaterialInfoAction extends BaseAction implements Preparable {
	private static final long serialVersionUID = -1231312312312312312L;

	private MaterialManager materialManager;
	private VipServiceManager vipServiceManager;
	private MaterialInfoManager materialInfoManager;
	private MajorManager majorManager;
	private SchoolManager schoolManager;
	private List<MaterialInfo> materialInfoes;
	private MaterialInfo materialInfo;
	private MaterialInfo materialInfoTemp;
    private VipService vipService;
	private Long id;
	private String query;
	private Long schoolId;
	private Long applyDegree;
	private Long applyMajorId;
	private Long vipServiceId;
	private Integer resultCode;
	private PageBean pageBean;
	private Material material; 

	/**
	 * Grab the entity from the database before populating with request
	 * parameters
	 */
	public void prepare() {
	}

	public String list() {
		vipService = vipServiceManager.get(vipServiceId);
		
		Material material = new Material();
		material.setVipService(vipService);
		
		materialInfo = new MaterialInfo();
		materialInfo.setMaterial(material);

		pageBean = materialInfoManager.findList(pageBean, materialInfo);
		return SUCCESS;
	}
	
    public String initEditMaterialInfo(){
		materialInfo = materialInfoManager.get(id);			
    	return SUCCESS; 
    }

    public String edit() throws Exception {
        try {
			MaterialInfo mi = materialInfoManager.get(id);
			
			mi.setApplyCost(materialInfoTemp.getApplyCost());
			mi.setApplyUrl(materialInfoTemp.getApplyUrl());
			mi.setCv(materialInfoTemp.getCv());
			mi.setEssay(materialInfoTemp.getEssay());
			mi.setGre(materialInfoTemp.getGre());
			mi.setPs(materialInfoTemp.getPs());
			mi.setToefl(materialInfoTemp.getToefl());
			mi.setRl(materialInfoTemp.getRl());
			mi.setSat(materialInfoTemp.getSat());
			mi.setSendAddress(materialInfoTemp.getSendAddress());
			mi.setSendTime(materialInfoTemp.getSendTime());
			mi.setApplyEndTime(materialInfoTemp.getApplyEndTime());
			
			materialInfoManager.saveOrUpdate(mi);
			resultCode = 1;
		} catch (Exception e) {
			e.printStackTrace();
			resultCode = 0;
		} 
		return SUCCESS;
    }

	public String save() throws Exception {
		try {
			Material material = new Material();
			material.setSchool(schoolManager.get(schoolId));
			material.setVipService(vipServiceManager.get(vipServiceId));
			material.setApplyDegree(applyDegree);
			material.setApplyMajor(majorManager.get(applyMajorId));
			material.setUser(getCurrentUser());
			materialInfo.setMaterial(material);
			material.setMaterialInfo(materialInfo);
			materialManager.save(material);
			resultCode = 1;
		} catch (Exception e) {
			e.printStackTrace();
			resultCode = 0;
			log.warn(e.getMessage());
		}
		return SUCCESS;
	}

	public String delete() {
        try {
        	materialManager.remove(id);
    		resultCode = 1;
		} catch (Exception e) {
			resultCode = 0;
			log.warn(e.getMessage());
		}

		return SUCCESS;
	}

	public String getMaterailBySid() {
		if (id != null) {
			materialInfo = materialInfoManager.get(id);
		} else {
			materialInfo = new MaterialInfo();
		}

		return SUCCESS;
	}

	public String addMaterialInfo() {
		if (id != null) {
			materialInfo = materialInfoManager.get(id);
		} else {
			materialInfo = new MaterialInfo();
		}

		return SUCCESS;
	}

	
	public MaterialManager getMaterialManager() {
		return materialManager;
	}

	public void setMaterialManager(MaterialManager materialManager) {
		this.materialManager = materialManager;
	}

	public VipServiceManager getVipServiceManager() {
		return vipServiceManager;
	}

	public void setVipServiceManager(VipServiceManager vipServiceManager) {
		this.vipServiceManager = vipServiceManager;
	}

	public MaterialInfoManager getMaterialInfoManager() {
		return materialInfoManager;
	}

	public void setMaterialInfoManager(MaterialInfoManager materialInfoManager) {
		this.materialInfoManager = materialInfoManager;
	}

	public MajorManager getMajorManager() {
		return majorManager;
	}

	public void setMajorManager(MajorManager majorManager) {
		this.majorManager = majorManager;
	}

	public SchoolManager getSchoolManager() {
		return schoolManager;
	}

	public Material getMaterial() {
		return material;
	}

	public void setMaterial(Material material) {
		this.material = material;
	}

	public void setSchoolManager(SchoolManager schoolManager) {
		this.schoolManager = schoolManager;
	}

	public List<MaterialInfo> getMaterialInfoes() {
		return materialInfoes;
	}

	public void setMaterialInfoes(List<MaterialInfo> materialInfoes) {
		this.materialInfoes = materialInfoes;
	}

	public MaterialInfo getMaterialInfo() {
		return materialInfo;
	}

	public void setMaterialInfo(MaterialInfo materialInfo) {
		this.materialInfo = materialInfo;
	}

	public MaterialInfo getMaterialInfoTemp() {
		return materialInfoTemp;
	}

	public void setMaterialInfoTemp(MaterialInfo materialInfoTemp) {
		this.materialInfoTemp = materialInfoTemp;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getQuery() {
		return query;
	}

	public void setQuery(String query) {
		this.query = query;
	}

	public PageBean getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}

	public Long getVipServiceId() {
		return vipServiceId;
	}

	public void setVipServiceId(Long vipServiceId) {
		this.vipServiceId = vipServiceId;
	}

	public Integer getResultCode() {
		return resultCode;
	}

	public void setResultCode(Integer resultCode) {
		this.resultCode = resultCode;
	}

	public Long getSchoolId() {
		return schoolId;
	}

	public void setSchoolId(Long schoolId) {
		this.schoolId = schoolId;
	}

	public Long getApplyDegree() {
		return applyDegree;
	}

	public void setApplyDegree(Long applyDegree) {
		this.applyDegree = applyDegree;
	}

	public Long getApplyMajorId() {
		return applyMajorId;
	}

	public void setApplyMajorId(Long applyMajorId) {
		this.applyMajorId = applyMajorId;
	}

	public VipService getVipService() {
		return vipService;
	}

	public void setVipService(VipService vipService) {
		this.vipService = vipService;
	}
	
}